package com.gongpingjialoan.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.CarBuyDetailActivity;
import com.gongpingjialoan.activity.sell.SellCarPrivateFragment;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.bean.SellPrivateBean;
import com.gongpingjialoan.utility.StepMonitor;

import java.util.List;

/**
 * Created by Administrator on 2016/9/5.
 */
public class SellCarPrivateAdapter extends BaseAdapter {

    private List<SellPrivateBean> mSellPrivateBeans;

    private SellCarPrivateFragment sellCarPrivateFragment;

    private Context mContext;

    private LayoutInflater mLayoutInflater;

    private boolean isEdit = false;

    public SellCarPrivateAdapter(List<SellPrivateBean> assessHistories, Context context , SellCarPrivateFragment sellCarPrivateFragment) {
        mSellPrivateBeans = assessHistories;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        this.sellCarPrivateFragment = sellCarPrivateFragment;
    }

    public void setData(List<SellPrivateBean> assessHistories) {
        mSellPrivateBeans = assessHistories;
        notifyDataSetChanged();
    }

    public void setEdit(boolean isEdit) {
        this.isEdit = isEdit;
        notifyDataSetChanged();

    }

    @Override
    public int getCount() {
        return mSellPrivateBeans.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.sell_car_history_item, null);
            holder = new ViewHolder();
            holder.car_status = (TextView) convertView.findViewById(R.id.status);
            holder.car_name = (TextView) convertView.findViewById(R.id.carName);
            holder.car_mile = (TextView) convertView.findViewById(R.id.mile);
            holder.car_time = (TextView) convertView.findViewById(R.id.time);
            holder.viewTextView = (TextView) convertView.findViewById(R.id.view_id);
            holder.collectTextView = (TextView) convertView.findViewById(R.id.collect_id);
            holder.selectImageView = (ImageView) convertView.findViewById(R.id.switch_id);

            holder.lineTextView = convertView.findViewById(R.id.line);
            holder.refreshTextView = (TextView) convertView.findViewById(R.id.refresh);
            holder.seeTextView = (TextView) convertView.findViewById(R.id.yulan);
            convertView.setTag(R.id.tag_first, holder);
        } else {
            holder = (ViewHolder) convertView.getTag(R.id.tag_first);
        }

        if (isEdit) {
            holder.selectImageView.setVisibility(View.VISIBLE);
        } else {
            holder.selectImageView.setVisibility(View.GONE);
            mSellPrivateBeans.get(position).isDelete = false;
        }
        if (mSellPrivateBeans.get(position).isDelete) {
            holder.selectImageView.setImageResource(R.drawable.checked);
        } else {
            holder.selectImageView.setImageResource(R.drawable.uncheck);
        }
        holder.viewTextView.setText(mSellPrivateBeans.get(position).getView_times() + "");
        holder.collectTextView.setText(mSellPrivateBeans.get(position).getLike_times() + "");
        holder.car_name.setText(mSellPrivateBeans.get(position).getTitle());
        holder.car_mile.setText(mSellPrivateBeans.get(position).getYear() + "上牌/" + mSellPrivateBeans.get(position).getMile() + "万公里");
        holder.car_status.setText(mSellPrivateBeans.get(position).getStatus_zh());
        holder.car_time.setText(mSellPrivateBeans.get(position).getPub_time());

        if(!"S".equals(mSellPrivateBeans.get(position).getStatus())){
            holder.car_status.setTextColor(mContext.getResources().getColor(R.color.discount_title));

            holder.seeTextView.setVisibility(View.GONE);
        }else{
            holder.car_status.setTextColor(mContext.getResources().getColor(R.color.discount_price_color));
            holder.seeTextView.setVisibility(View.VISIBLE);
        }

        if(mSellPrivateBeans.get(position).isCar_refresh()){
            holder.refreshTextView.setVisibility(View.VISIBLE);
        }else{
            holder.refreshTextView.setVisibility(View.GONE);
        }

        if(holder.refreshTextView.getVisibility() == View.VISIBLE && holder.seeTextView.getVisibility() == View.VISIBLE){
            holder.lineTextView.setVisibility(View.VISIBLE);
        }else{
            holder.lineTextView.setVisibility(View.GONE);
        }

        holder.refreshTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StepMonitor.getInstance().addMonitor(new Monitor("click", "my_sellRecord_refresh", System.currentTimeMillis(), 1f));
                sellCarPrivateFragment.refreshItem(mSellPrivateBeans.get(position).getId());
            }
        });

        holder.seeTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(mContext, CarBuyDetailActivity.class);
                intent.putExtra("typevalue", mSellPrivateBeans.get(position).getCar_id());
                intent.putExtra("city", mSellPrivateBeans.get(position).getCity());
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }


    public static class ViewHolder {
        public TextView car_name;
        public TextView car_mile;
        public TextView car_status;
        public TextView car_time;
        public View lineTextView;
        public TextView refreshTextView;
        public TextView seeTextView;
        public TextView viewTextView;
        public TextView collectTextView;
        public ImageView selectImageView;

    }
}
